package com.itbour.web.action.yjx.trade;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.swing.filechooser.FileSystemView;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.tg.DisUserdocDto;
import com.itbour.dto.video.VideoDesignDto;
import com.itbour.dto.yjx.trade.YjxCustAniDto;
import com.itbour.dto.yjx.trade.YjxVideoUserDocDto;
import com.itbour.service.yjx.trade.YjxCustAniService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 	@author 李琦
 *	  动画管理
 */
public class GetCustAniAction extends BaseAction {

	/**
	 */
	private static final long serialVersionUID = 1L;
	private YjxCustAniService yjxCustAniService;
	private String findAniId;// 动画ID
	private String findName;// 动画名称
	private String findUserId;// 用户ID
	private String findPri;// 场景分类
	private Map<String, Object> resMap =new HashMap<String, Object>();
	
	// 用户视频作品
	private String findDocName;// 作品名称
	
	
	/* 获取动画列表信息
	 * 参数：page/rows
	 * 作者：李琦
	 */
	public String getCustAniInfo(){
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if (user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			Map<String, Object> param = new HashMap<String, Object>();
			if (StringUtils.isNotEmpty(findAniId)){
				param.put("ani_id",findAniId);
			}
			if (StringUtils.isNotEmpty(findName)){
				param.put("ani_name",findName);
			}
			if (StringUtils.isNotEmpty(findUserId)){
				param.put("user_id",findUserId);
			}
			if (StringUtils.isNotEmpty(findPri)){
				param.put("private",findPri);
			}
			Page page = yjxCustAniService.getCustAniInfo(param,
					Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			resMap.put("total", page.getTotalCount());
			resMap.put("rows", page.getResult());
    	}		
		return SUCCESS;
	}
	public String getFindName() {
		return findName;
	}
	public void setFindName(String findName) {
		this.findName = findName;
	}
	public String getFindUserId() {
		return findUserId;
	}
	public void setFindUserId(String findUserId) {
		this.findUserId = findUserId;
	}
	public String getFindPri() {
		return findPri;
	}
	public void setFindPri(String findPri) {
		this.findPri = findPri;
	}
	
	
	
	
	private YjxCustAniDto custAni;
	public void addCustAniInfo() throws Exception{
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if (user!=null && user.getUser_id()!=null){
    		custAni.setUser_id(user.getUser_id());
    		boolean b = yjxCustAniService.insertCustAni(custAni);
    		if (b){
    			this.writeJSON("ok"); 
    		} else {
    			this.writeJSON("no"); 
    		}
    	} else {
    		this.writeJSON("no");
    	}
	}
	
	public void delCustAniInfo() throws Exception{
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		if (user!=null && user.getUser_id()!=null && ids!=null){
			Map<String, Object> parm =new HashMap<String, Object>();
			String[] st = StringUtils.str2Array(ids);
			parm.put("ids", StringUtils.array2String(st));
			parm.put("update_by", user.getNickname()+"*"+user.getUser_id());
			boolean b = yjxCustAniService.delCustAni(parm);
			if (b){
				this.writeJSON("ok"); 
			} else {
				this.writeJSON("no"); 
			}
		} else {
			this.writeJSON("no");
		}
	}
	
	// 获取单个 动画信息
	private List<YjxCustAniDto> custLis;
	public String getOneCustInfo(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		HttpServletRequest request = ServletActionContext.getRequest();
		String aniId = request.getParameter("aniId");
		if (user!=null && user.getUser_id()!=null && aniId!=null){
			Map<String, Object> parm =new HashMap<String, Object>();
			parm.put("ani_id",aniId);
			custLis=yjxCustAniService.getOneCustInfo(parm);
		}
		return SUCCESS;
	}
	
	private String findDocId;// 用户作品ID
	private String findUniDocId;// 统一作品ID查询
	// 获取 用户视频作品列表
	public String getVideoUserDoc(){
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if (user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			Map<String, Object> param = new HashMap<String, Object>();
			String pageSize = request.getParameter("rows");
			if (StringUtils.isNotEmpty(findDocName)){
				param.put("doc_name",findDocName);
				pageNumber = "1";
				pageSize = "1";
			}
			if (StringUtils.isNotEmpty(findDocId)){
				param.put("doc_id", findDocId);
			}
			if (StringUtils.isNotEmpty(findUniDocId)){
				param.put("uni_doc_id", findUniDocId);
			}
			
			Page page = yjxCustAniService.getVideoUserDoc(param,
					Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			if (resMap!=null){
				resMap.clear();
			}
			resMap.put("total", page.getTotalCount());
			resMap.put("rows", page.getResult());
    	}		
		return SUCCESS;
	}

	// 获取 一条用户 视频作品信息
	private List<YjxVideoUserDocDto> videoUserDocList;
	public String getOneVideoUserDoc(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if (user!=null && user.getUser_id()!=null){
			Map<String, Object> param = new HashMap<String, Object>();
			HttpServletRequest request = ServletActionContext.getRequest();
			String uniDocId = request.getParameter("uniDocId");
			param.put("uni_doc_id", uniDocId);
			videoUserDocList = yjxCustAniService.getOneVideoUserDoc(param);
		}		
		return SUCCESS;
	}
	
	// 下载到本地Json文件
	static String path = "\\yjxDocJson\\";
	static String TGDATA = "\\tg_data";// 提纲数据
	static String VDATA = "\\v_data";// 视频数据
	static String TMPLDATA = "\\tmp_data";// 作品数据
	private Object ret;
	public String downLocalJsonFile(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if (user!=null && user.getUser_id()!=null){
			Map<String, Object> param = new HashMap<String, Object>();
			HttpServletRequest request = ServletActionContext.getRequest();
			String uniDocId = request.getParameter("uniDocId");
			param.put("uni_doc_id", uniDocId);
			List<YjxVideoUserDocDto> videoUserDocList = yjxCustAniService.getOneVideoUserDoc(param);
			
			if (videoUserDocList.size()<=0){
				ret = renderError("JSON数据为空 暂无下载");
				return SUCCESS;
			}
			
			FileSystemView sys = FileSystemView.getFileSystemView();
			File file = sys.getHomeDirectory();
			String tmp = file.getPath();
			
			Date date = new Date();// 获取当前时间
			SimpleDateFormat sim = new SimpleDateFormat("yyyyMMddHHmmss");
			System.out.println(sim.format(date));
			String tmpDate = sim.format(date);
			String paString = tmp+path+tmpDate;
			
			delFolder(tmp+path);// 清除所有文件夹
			File localFile =new File(paString);
			// 判断父目录不存在
			if (!localFile.exists()){
				localFile.mkdirs();// 创建文件夹
			}
			
			String tg_data = "";
			String v_data = "";
			String tmpl_data = "";
			
			for (YjxVideoUserDocDto dto : videoUserDocList) {
				tg_data = dto.getTg_data();
				v_data = dto.getV_data();
				tmpl_data = dto.getTmpl_json();
			}
			
			try {
				String tgPath =paString+TGDATA+".json";
				String vPath = paString+VDATA+".json";
				String tmplPath = paString+TMPLDATA+".json";
				createNewFile(tg_data, tgPath);
				createNewFile(v_data, vPath);
				createNewFile(tmpl_data, tmplPath);
				ret = renderSucc("下载成功,JSON文件已保存桌面yjxDocJson文件夹中");
			} catch (Exception e) {
				ret = renderError("下载JSON失败");
			}
		} else {
			ret = renderError("参数为空");
		}	
		return SUCCESS;
	}
	
	public static void createNewFile(String data,String realPath){
		File localFile =new File(realPath);
		if(!localFile.exists()){
			try {
				localFile.createNewFile();
				System.out.println("创建了新的文件");
			} catch (IOException e) {
				e.printStackTrace();
			}// 创建新文件
		} else {
			System.out.println("文件已存在");
		}
		// 写入数据
		FileOutputStream fiOutputStream = null;
		PrintStream printStream = null;
		
		try {
			fiOutputStream = new FileOutputStream(localFile);
			printStream = new PrintStream(fiOutputStream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}// 文件输出流
		
		printStream.print(data);
		printStream.close();
	}
	
	public static boolean delAllFile(String path) {// 删除文件
	       boolean flag = false;
	       File file = new File(path);
	       if (!file.exists()) {
	         return flag;
	       }
	       if (!file.isDirectory()) {
	         return flag;
	       }
	       String[] tempList = file.list();
	       File temp = null;
	       for (int i = 0; i < tempList.length; i++) {
	          if (path.endsWith(File.separator)) {
	             temp = new File(path + tempList[i]);
	          } else {
	              temp = new File(path + File.separator + tempList[i]);
	          }
	          if (temp.isFile()) {
	             temp.delete();
	          }
	          if (temp.isDirectory()) {
	             delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
	             delFolder(path + "/" + tempList[i]);//再删除空文件夹
	             flag = true;
	          }
	       }
	       return flag;
	}
	
	public static void delFolder(String folderPath) {// 删除文件夹
	     try {
	        delAllFile(folderPath); //删除完里面所有内容
	        String filePath = folderPath;
	        filePath = filePath.toString();
	        java.io.File myFilePath = new java.io.File(filePath);
	        myFilePath.delete(); //删除空文件夹
	     } catch (Exception e) {
	       e.printStackTrace(); 
	     }
	}
	
	
	
	// 用户自由创建作品
	public String getCustVideoDoc(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if (user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			Map<String, Object> param = new HashMap<String, Object>();
			String pageSize = request.getParameter("rows");
			if (StringUtils.isNotEmpty(findDocName)) {
				param.put("doc_name",findDocName);
				pageNumber = "1";
				pageSize = "1";
			}
			if (StringUtils.isNotEmpty(findDocId)) {
				param.put("doc_id", findDocId);
			}
			if (StringUtils.isNotEmpty(findUniDocId)) {
				param.put("uni_doc_id", findUniDocId);
			}
			
			Page page = yjxCustAniService.getCustVideoDoc(param,
					Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			if (resMap!=null){
				resMap.clear();
			}
			resMap.put("total", page.getTotalCount());
			resMap.put("rows", page.getResult());
		}		
		return SUCCESS;
	}
	
	public String getOneCustVideoUserDoc(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if (user!=null && user.getUser_id()!=null){
			Map<String, Object> param = new HashMap<String, Object>();
			HttpServletRequest request = ServletActionContext.getRequest();
			String uniDocId = request.getParameter("uniDocId");
			param.put("uni_doc_id", uniDocId);
			videoUserDocList = yjxCustAniService.getOneVideoDocData(param);
		}		
		return SUCCESS;
	}
	
	/** *****************************  获取设计作品的data数据 做分析
	 */
	private String findDisUserId;
	private String findMobile;// 用户手机号
	Map<String, Object> dUserDocMap = new HashMap<String, Object>();//
	public String getDesignUserDoc(){
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if (user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			Map<String, Object> param = new HashMap<String, Object>();
			String pageSize = request.getParameter("rows");
			if (StringUtils.isNotEmpty(findDocName)){
				param.put("doc_name",findDocName);
				pageNumber = "1";
				pageSize = "1";
			}
			if (StringUtils.isNotEmpty(findDocId)){
				param.put("doc_id", findDocId);
			}
			if (StringUtils.isNotEmpty(findDisUserId)){
				param.put("user_id", findDisUserId);
			}
			if (StringUtils.isNotEmpty(findMobile)){
				param.put("mobile", findMobile);
			}
			
			Page page = yjxCustAniService.getDesignUserDoc(param,
					Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			dUserDocMap.put("total", page.getTotalCount());
			dUserDocMap.put("rows", page.getResult());
    	}		
		return SUCCESS;
	}
	
	/**
	 *  获取单个  作品的data和设计模板的data
	 */
	private List<DisUserdocDto> disUserDoc;
	public String getOneDesignUserDoc(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if (user!=null && user.getUser_id()!=null){
			Map<String, Object> param = new HashMap<String, Object>();
			HttpServletRequest request = ServletActionContext.getRequest();
			String docId = request.getParameter("docId");
			param.put("doc_id", docId);
			disUserDoc = yjxCustAniService.getOneDesignUserDoc(param);
		}		
		return SUCCESS;
	}
	
	
	/** *****************************  获取视频模板列表
	 */
	private String findVname;
	private String findVdesignId;//
	public String getVideoTmplData(){
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if (user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			
			Map<String, Object> param = new HashMap<String, Object>();
			if (StringUtils.isNotEmpty(findVdesignId)){
				param.put("design_id", findVdesignId);
			}
			if (StringUtils.isNotEmpty(findVname)){
				param.put("design_name", findVname);
			}
			Page page = yjxCustAniService.getVideoTmpl(param,
					Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			dUserDocMap.put("total", page.getTotalCount());
			dUserDocMap.put("rows", page.getResult());
    	}		
		return SUCCESS;
	}
	
	/** 获取单个 视频模板的Data
	 */
	private List<VideoDesignDto> videoData;
	public String getOneVideoDataInfo(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if (user!=null && user.getUser_id()!=null){
			Map<String, Object> param = new HashMap<String, Object>();
			HttpServletRequest request = ServletActionContext.getRequest();
			String designId = request.getParameter("designId");
			param.put("design_id", designId);
			videoData = yjxCustAniService.getOneVideoDataInfo(param);
		}		
		return SUCCESS;
	}
	
	
	/** 获取 VIP用户作品
	 */
	private String findVip;
	public String getVipDesignUserDoc(){
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	if (user!=null && user.getUser_id()!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			Map<String, Object> param = new HashMap<String, Object>();
			if (StringUtils.isNotEmpty(findVip)){
				param.put("vip", findVip);
			}
			Page page = yjxCustAniService.getVipDesignUserDoc(param,
					Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			dUserDocMap.put("total", page.getTotalCount());
			dUserDocMap.put("rows", page.getResult());
    	}		
		return SUCCESS;
	}
	
	
	
	//   Set和GET
	
	public YjxCustAniDto getCustAni() {
		return custAni;
	}
	public String getFindVip() {
		return findVip;
	}
	public void setFindVip(String findVip) {
		this.findVip = findVip;
	}
	public String getFindVname() {
		return findVname;
	}
	public void setFindVname(String findVname) {
		this.findVname = findVname;
	}
	public String getFindVdesignId() {
		return findVdesignId;
	}
	public void setFindVdesignId(String findVdesignId) {
		this.findVdesignId = findVdesignId;
	}
	public List<VideoDesignDto> getVideoData() {
		return videoData;
	}
	public void setVideoData(List<VideoDesignDto> videoData) {
		this.videoData = videoData;
	}
	public Map<String, Object> getdUserDocMap() {
		return dUserDocMap;
	}
	public void setdUserDocMap(Map<String, Object> dUserDocMap) {
		this.dUserDocMap = dUserDocMap;
	}
	public List<YjxCustAniDto> getCustLis() {
		return custLis;
	}
	public void setCustLis(List<YjxCustAniDto> custLis) {
		this.custLis = custLis;
	}
	public void setCustAni(YjxCustAniDto custAni) {
		this.custAni = custAni;
	}
	public YjxCustAniService getYjxCustAniService() {
		return yjxCustAniService;
	}
	public void setYjxCustAniService(YjxCustAniService yjxCustAniService) {
		this.yjxCustAniService = yjxCustAniService;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public Map<String, Object> getResMap() {
		return resMap;
	}
	public void setResMap(Map<String, Object> resMap) {
		this.resMap = resMap;
	}
	public String getFindAniId() {
		return findAniId;
	}
	public void setFindAniId(String findAniId) {
		this.findAniId = findAniId;
	}
	public String getFindUniDocId() {
		return findUniDocId;
	}
	public void setFindUniDocId(String findUniDocId) {
		this.findUniDocId = findUniDocId;
	}
	public String getFindDocName() {
		return findDocName;
	}
	public void setFindDocName(String findDocName) {
		this.findDocName = findDocName;
	}
	public String getFindDocId() {
		return findDocId;
	}
	public void setFindDocId(String findDocId) {
		this.findDocId = findDocId;
	}
	public List<YjxVideoUserDocDto> getVideoUserDocList() {
		return videoUserDocList;
	}
	public void setVideoUserDocList(List<YjxVideoUserDocDto> videoUserDocList) {
		this.videoUserDocList = videoUserDocList;
	}
	public List<DisUserdocDto> getDisUserDoc() {
		return disUserDoc;
	}
	public void setDisUserDoc(List<DisUserdocDto> disUserDoc) {
		this.disUserDoc = disUserDoc;
	}
	public String getFindDisUserId() {
		return findDisUserId;
	}
	public void setFindDisUserId(String findDisUserId) {
		this.findDisUserId = findDisUserId;
	}
	public String getFindMobile() {
		return findMobile;
	}
	public void setFindMobile(String findMobile) {
		this.findMobile = findMobile;
	}
	public Object getRet() {
		return ret;
	}
	public void setRet(Object ret) {
		this.ret = ret;
	}
}
